package com.example.lib_base.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.annotation.LayoutRes
import androidx.core.view.isVisible
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.lib_base.utils.clickWithTrigger

/*
*
* 相较于基础的RecycleView.Adapter
* 基于ListAdapter或Paging的PagingAdapter都有提交数据的api  "submitList"
*
* 后面如果封装一个完整的有各需求的adapter可参考BaseQuickAdapter的这个快速构建adapter的库
* */

abstract class BasePagingAdapter<T : Any>(private var diffCallback: DiffUtil.ItemCallback<T>) :
    PagingDataAdapter<T, RecyclerView.ViewHolder>(diffCallback) {

    protected lateinit var context: Context
        private set

    private var mItemClickListener: ItemClickListener? = null
    private var mItemChild: ItemChildCLickListener? = null

    companion object {
        private const val TAG = "BasePagingAdapter"
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val item = getItem(position) ?: return
        val baseHolder = (holder as BasePagingAdapter<*>.BaseViewHolder)
        baseHolder.bindNormalData(item)
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val holder = BaseViewHolder(parent, viewType)
        this.context = parent.context
        //Item的点击事件
        holder.itemView.clickWithTrigger {
            mItemClickListener?.let {
                mItemClickListener!!.itemClick(getItem(holder.layoutPosition) as T)
            }
        }
        //ItemView 子view的点击事件
        getClickViewId()?.let {
            if (it.isNotEmpty() && mItemChild != null) {
                it.forEach { viewId ->
                    holder.getHelper().setClickListener(viewId, mItemChild!!)
                }
            }
        }
        return holder
    }

    override fun getItemViewType(@LayoutRes position: Int): Int {
        return getItemLayout(position)
    }


    /**
     * 子类获取layout
     */
    protected abstract fun getItemLayout(position: Int): Int

    /**
     * 设置点击的子view id
     */

    protected abstract fun getClickViewId(): Array<Int>?

    /**
     * itemView的点击事件
     */
    open fun setOnItemClick(listener: ItemClickListener) {
        this.mItemClickListener = listener
    }

    /**
     * itemView的子view的点击事件
     */
    open fun setOnItemChildClick(listener: ItemChildCLickListener) {
        this.mItemChild = listener
    }

    /**
     * 子类绑定数据
     */
    protected abstract fun bindData(helper: ItemHelper, data: T?)


    inner class BaseViewHolder(parent: ViewGroup, layout: Int) : RecyclerView.ViewHolder(
        LayoutInflater.from(parent.context).inflate(layout, parent, false)
    ) {
        private val helper: ItemHelper = ItemHelper(this)
        fun bindNormalData(item: Any?) {
            bindData(helper, item as T)
        }

        fun getHelper(): ItemHelper {
            return helper
        }
    }


    /**
     * ItemView的辅助类
     */
    inner class ItemHelper(private val holder: BasePagingAdapter<*>.BaseViewHolder) {
        private val itemView = holder.itemView
        private val viewCache = SparseArray<View>()

        private fun findViewById(viewId: Int): View {
            var view = viewCache.get(viewId)
            if (view == null) {
                view = itemView.findViewById(viewId)
                if (view == null) {
                    throw NullPointerException("$viewId can not find this item！")
                }
                viewCache.put(viewId, view)
            }
            return view
        }

        /**
         * TextView设置内容
         */
        fun setText(viewId: Int, text: CharSequence?): ItemHelper {
            (findViewById(viewId) as TextView).text = text
            return this
        }

        /**
         * 子view点击事件
         */
        fun setClickListener(viewId: Int, clickListener: ItemChildCLickListener): ItemHelper {
            if (itemCount > 0) {
                findViewById(viewId).setOnClickListener {
                    clickListener.itemClick(viewId, holder.layoutPosition)
                }
            }
            return this
        }

        /*
        * 设置是否可见
        * */
        fun setVisible(viewId: Int, visible: Boolean): ItemHelper {
            (findViewById(viewId) as View).isVisible = visible
            return this
        }

        @SuppressLint("UseCompatLoadingForDrawables")
        fun setBackGround(viewId: Int, @DrawableRes backGround: Int): ItemHelper {
            (findViewById(viewId) as View).background = context.getDrawable(backGround)
            return this
        }

        /**
         * Coil加载图片
         */
//        fun bindImgCoil(viewId: Int, url: String) {
//            val imageView: ImageView = findViewById(viewId) as ImageView
//            imageView.load(url) {
//                placeholder(R.mipmap.img_placeholder)
//            }
//
//        }

        /**
         * Glide加载图片
         */
        fun bindImgGlide(viewId: Int, @DrawableRes url: Int) {
            val imageView: ImageView = findViewById(viewId) as ImageView
            Glide.with(context)
                .load(url)
                .into(imageView)
        }
    }

}